package com.alogic.xscript.plugins;

import com.anysoft.formula.Function;
import com.anysoft.util.Settings;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;

import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.AbstractLogiclet;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.anysoft.formula.DefaultFunctionHelper;
import com.anysoft.formula.Expression;
import com.anysoft.formula.Parser;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;

/**
 * 将指定公式的计算值设置到指定上下文变量
 * 
 * @author duanyy
 * @version 1.6.8.14 [20170509 duanyy] <br>
 * - 增加xscript的中间文档模型,以便支持多种报文协议 <br>
 *
 * @version 1.6.13.23 [20201030 duanyy] <br>
 * - 优化xscript的日志机制  <br>
 */
public class Formula extends AbstractLogiclet {
	protected String id;
	protected Expression expr = null;
	protected String dftValue = "";
	
	public Formula(String tag, Logiclet p) {
		super(tag, p);
	}

	public void configure(Properties p){
		super.configure(p);
		
		id = PropertiesConstants.getString(p,"id","",true);
		
		String formula = PropertiesConstants.getString(p,"expr","",true);
		if (StringUtils.isNotEmpty(formula)){
			Parser parser = new Parser(new DefaultFunctionHelper(null));			
			expr = parser.parse(formula);
		}
	}

	@Override
	protected void onExecute(XsObject root,XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
		if (expr != null){
			try {
				String value = expr.getValue(ctx).toString();
				ctx.SetValue(id, value);
			}catch (Exception ex){
				log(String.format("Failed to execute expression:%s,error:%s",expr.toString(),ex.getMessage()));
			}
		}
	}

	/**
	 * Function插件注册
	 */
	public class FunctionPlugin extends AbstractLogiclet {
		protected String $name = "";
		protected String $module = "";

		public FunctionPlugin(String tag, Logiclet p) {
			super(tag, p);
		}

		public void configure(Properties p){
			super.configure(p);
			$name = PropertiesConstants.getRaw(p,"name",$name);
			$module = PropertiesConstants.getRaw(p,"module",$module);
		}

		@Override
		protected void onExecute(XsObject root, XsObject current, LogicletContext ctx,
								 ExecuteWatcher watcher) {
			String name = PropertiesConstants.transform(ctx,$name,"");
			String module = PropertiesConstants.transform(ctx,$module,"");
			if (StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(module)){
				ClassLoader cl = Settings.getClassLoader();
				cl = cl != null ? cl : Thread.currentThread().getContextClassLoader();
				Class<Function> clazz = null;
				try {
					clazz = (Class<Function>)cl.loadClass(module);
					DefaultFunctionHelper.addFunction(name,clazz);
				} catch (ClassNotFoundException e) {
					log("Can not find class " + module);
				}
			}
		}
	}
}
